package epam_java.jsontest;

import java.util.StringTokenizer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ParcerFactory {
    /**
     * @param str line from json format file
     * with integral value
     * @return
     */
  public  static int parceInt(String str) throws JsonParserException{
             Pattern p=Pattern.compile("^\\\"([a-zA-z])+\\\":\\s*(\\d+)\\,$");
            Matcher matcher = p.matcher(str);
            int i=0;
            String intvalue="";
            try{
           if(matcher.find()){
                intvalue=matcher.group(2);
                i=Integer.parseInt(intvalue);
            } else{
               throw new JsonParserException("Cannot find int value");
            }
            }catch (NumberFormatException ex){
                throw new JsonParserException("Faild function parseInt with value= "
                        + intvalue);
           }
            return i;
  }
    public  static long parceLong(String str) throws JsonParserException{
             Pattern p=Pattern.compile("^\\\"([a-zA-z])+\\\":\\s*(\\d+)\\,$");
            Matcher matcher = p.matcher(str);
            long i=0;
            String longvalue="";
            try{
           if(matcher.find()){
                longvalue=matcher.group(2);
                i=Long.parseLong(longvalue);
            } else{
               throw new JsonParserException("Cannot find int value");
            }
            }catch (NumberFormatException ex){
                throw new JsonParserException("Faild function parseInt with value= "
                        + longvalue);
           }
            return i;
  }
      /**
     * @param str line from json format file
     * with integral value
     * @return
     */
  public  static double parceDouble(String str) throws JsonParserException{
            Pattern p=Pattern.compile("^\\\"([a-zA-z])+\\\":\\s*(\\d+[\\.|\\,]\\d+)\\,$");
            Matcher matcher = p.matcher(str);
            double i=0;
            String doublevalue="";
            try{
           if(matcher.find()){
                doublevalue=matcher.group(2);
                doublevalue.replace(',', '.');
                i=Double.parseDouble(doublevalue);
            } else{
               throw new JsonParserException("Cannot find double value");
            }
            }catch (NumberFormatException ex){
                throw new JsonParserException("Faild function parseDouble with value= "
                        + doublevalue);
           }
            return i;
  }
    public  static String parceString(String str) throws JsonParserException{
            Pattern p=Pattern.compile("^\\\"([a-zA-z])+\\\":\\s*\\\"(.*)\\\"\\,*$");
            Matcher matcher = p.matcher(str);
            String value="";
           if(matcher.find()){
                value=matcher.group(2);
            } else{
               throw new JsonParserException("Cannot find String value");
            }
            return value;
  }
        public  static boolean parceBoolean(String str) throws JsonParserException{
            Pattern p=Pattern.compile("^\\\"([a-zA-z])+\\\":\\s*(true|false)\\,$");
            Matcher matcher = p.matcher(str);
            String value="";
           if(matcher.find()){
                value=matcher.group(2);
            } else{
               throw new JsonParserException("Cannot find Boolean value");
            }
            return (value.compareTo("true")==0);
  }
                public  static char parceChar(String str) throws JsonParserException{
            Pattern p=Pattern.compile("^\\\"([a-zA-z])+\\\":\\s*\\\'(.)\\\'\\,$");
            Matcher matcher = p.matcher(str);
            String value="";
           if(matcher.find()){
                value=matcher.group(2);
            } else{
               throw new JsonParserException("Cannot find Boolean value");
            }
            return value.charAt(0);
  }
  public  static double[] parceDoubleArray(String str) throws JsonParserException{
      str=str.substring(str.lastIndexOf('[')+1,str.lastIndexOf(']'));
      StringTokenizer t=new StringTokenizer(str," ,");
      double[] arr=new double[t.countTokens()];
      String token="";
      int i=0;
      while(t.hasMoreTokens()){
          try{
          token=t.nextToken();
          arr[i]=Double.parseDouble(token);
          } catch(NumberFormatException ex){
              throw new JsonParserException("Fail double format "+ str);
          }
          i++;
      }
      return arr;
  }
  public  static int[] parceIntArray(String str) throws JsonParserException{
      str=str.substring(str.lastIndexOf('[')+1,str.lastIndexOf(']'));
      StringTokenizer t=new StringTokenizer(str," ,");
      int[] arr=new int[t.countTokens()];
      String token="";
      int i=0;
      while(t.hasMoreTokens()){
          try{
          token=t.nextToken();
          arr[i]=Integer.parseInt(token);
          } catch(NumberFormatException ex){
              throw new JsonParserException("Fail Integer format "+ str);
          }
          i++;
      }
      return arr;
  }
   public  static String[] parceStringArray(String str) throws JsonParserException{
      str=str.substring(str.lastIndexOf('[')+1,str.lastIndexOf(']'));
      StringTokenizer t=new StringTokenizer(str," ,\"");
      String[] arr=new String[t.countTokens()];
      String token="";
      int i=0;
      while(t.hasMoreTokens()){
          try{
          token=t.nextToken();
          arr[i]=token;
          } catch(NumberFormatException ex){
              throw new JsonParserException("Fail String format "+ str);
          }
          i++;
      }
      return arr;
  }
}
